package com.examiner.controller;

import com.examiner.bean.Department;
import com.examiner.bean.Job;
import com.examiner.result.EmptyObject;
import com.examiner.result.Result;
import com.examiner.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    /**
     * 新增部门
     * @param department
     * @return
     */
    @PostMapping("/addDepartment")
    public Result<Object> addDepartment(@RequestBody Department department){
        // 判断当前部门名是否存在
        Department existByName =  departmentService.findByUserName(department.getUserName());
        // 存在则 报错
        if (existByName != null){
            return Result.RESULT(0,"该部门已存在",false,EmptyObject.empty);
        }
        // 不存在，进行存储
        department.setEid(1);
        department.setExaminer("e1");
        Department addResult = departmentService.addDepartment(department);
        return Result.SUCCESS(EmptyObject.empty);
    }

    /**
     * 删除部门
     * @return
     */
    @PostMapping("/delDepartment/{id}")
    public Result<Object> delDepartment(@PathVariable("id")Integer id){
        // 判断该用户是否存在
        Boolean existsById = departmentService.existsById(id);
        if (!existsById) return Result.RESULT(1008,"当前部门不存在",false,null);
        Boolean check = departmentService.removeDepartment(id);
        // 判断是否删除成功
        if (check) return Result.RESULT(1007,"删除失败",false,null);
        return Result.SUCCESS(EmptyObject.empty);
    }

    /**
     * 修改部门信息
     * @return
     */
    @PostMapping("/editDepartment")
    public Result<Object> editDepartment(@RequestBody Department department){
        // todo 判断当前用户是否存在
        Boolean existsById = departmentService.existsById(department.getId());
        // 判断该用户名是否被使用
        if (!existsById){
            return Result.RESULT(1002,"该部门不存在",false,null);
        }
        // 修改信息
        Department reCom = departmentService.changeDepartmentInfo(department);
        return Result.SUCCESS(EmptyObject.empty);
    }

    /**
     * 根据id查询当前职位
     * @param id
     * @return
     */
    @PostMapping("/getDepartmentById/{id}")
    public Result<Object> getDepartmentById(@PathVariable("id")Integer id){
        Department department;
        try{
            department = departmentService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"获取部门信息失败",false,null);
        }
        if (department == null){
            return Result.RESULT(201,"当前部门不存在",false,null);
        }
        return Result.RESULT(200,"成功获取部门信息",true,department);
    }




}
